package com.gerryrun.itemscrollapplication

import android.app.Activity
import android.os.Bundle
import android.view.LayoutInflater
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.gerryrun.itemscrollapplication.databinding.ActivityMainBinding
import com.gerryrun.itemscrollapplication.simple.IItemClickCallBack
import com.gerryrun.itemscrollapplication.simple.MyAdapter
import com.gerryrun.itemscrollapplication.simple.MyData
import com.gerryrun.itemscrollapplication.simple.SubItem

class MainActivity : AppCompatActivity() {
    private lateinit var mainBinding: ActivityMainBinding
    private lateinit var mActivity: Activity
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mainBinding = ActivityMainBinding.inflate(LayoutInflater.from(this))
        setContentView(mainBinding.root)
        mActivity = this

        testRecyclerView1()
    }
    private fun testRecyclerView1() {
        val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
        recyclerView.setLayoutManager(LinearLayoutManager(this))

        val data = listOf(
            MyData("Item 1", listOf(SubItem("SubItem 1.1"), SubItem("SubItem 1.2"))),
            MyData("Item 2", listOf(SubItem("SubItem 2.1"), SubItem("SubItem 2.2"))),
            MyData("Item 3", listOf(SubItem("SubItem 3.1"), SubItem("SubItem 3.2"))),
            MyData("Item 1", listOf(SubItem("SubItem 1.1"), SubItem("SubItem 1.2"))),
            MyData("Item 2", listOf(SubItem("SubItem 2.1"), SubItem("SubItem 2.2"))),
            MyData("Item 3", listOf(SubItem("SubItem 3.1"), SubItem("SubItem 3.2"))),
            MyData("Item 2", listOf(SubItem("SubItem 2.1"), SubItem("SubItem 2.2"))),
            MyData("Item 3", listOf(SubItem("SubItem 3.1"), SubItem("SubItem 3.2"))),
            MyData("Item 1", listOf(SubItem("SubItem 1.1"), SubItem("SubItem 1.2"))),
            MyData("Item 2", listOf(SubItem("SubItem 2.1"), SubItem("SubItem 2.2"))),
            MyData("Item 3", listOf(SubItem("SubItem 3.1"), SubItem("SubItem 3.2")))
        )

        val adapter = MyAdapter(data, object : IItemClickCallBack {
            override fun itemClickEdit(position: Int, data: Any) {

                showToast(mActivity, "编辑:$position")
            }

            override fun itemClickDelete(position: Int, data: Any) {
                showToast(mActivity, "删除:$position")
            }

            override fun childItemClick(parentPosition: Int, childPosition: Int, data: Any) {

                showToast(mActivity, "childItemClick:$parentPosition")
            }
        })
        recyclerView.adapter = adapter
    }

    private fun showToast(mActivity: Activity, s: String) {

        Toast.makeText(mActivity, "", Toast.LENGTH_SHORT).show()
    }
}